//
//  MusicPlayViewController.m
//  享动
//
//  Created by darker on 16/4/9.
//  Copyright © 2016年 HHUI. All rights reserved.
//

#import "MusicPlayViewController.h"
#import "PlayManager.h"
@interface MusicPlayViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *startLabel;
@property (weak, nonatomic) IBOutlet UISlider *slider;
@property (weak, nonatomic) IBOutlet UILabel *endLabel;


@property (weak, nonatomic) IBOutlet UIButton *lastButton;

@property (weak, nonatomic) IBOutlet UIButton *nextButton;

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@property (weak, nonatomic) IBOutlet UILabel *author;
@property (weak, nonatomic) IBOutlet UILabel *content;


@property (strong, nonatomic) IBOutlet UIView *bigView;

@property(nonatomic,assign)NSInteger playIndex;

@property(nonatomic,strong)NSTimer * timer;

@property(nonatomic,strong)NSMutableArray * musicUrlArray;

@property(nonatomic,assign)BOOL isFirstPlay;

@end

@implementation MusicPlayViewController



- (void)viewDidLoad {
    
    [super viewDidLoad];
    
     [self getView];
    
    
    // 赋值音乐播放链接数组
    _musicUrlArray = [NSMutableArray array];
    
    for (musicRunModel * model in _modelArray) {
        [_musicUrlArray addObject:model.url];
    }
   
    
    // 赋值给单例
    [PlayManager sharedManager].index = _index;
    [[PlayManager sharedManager]setMusicArray:_musicUrlArray];
   
    

    [_startButton addTarget:self action:@selector(play) forControlEvents:UIControlEventTouchUpInside];
    [_lastButton addTarget:self action:@selector(last) forControlEvents:UIControlEventTouchUpInside];
    [_nextButton addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside];
    
    _timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(playerPlaying) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
    
    
}
- (void)play
{
    if ([[PlayManager sharedManager]isPlaying]) {
        [[PlayManager sharedManager]pause];
        [self.startButton setImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];
        [_timer setFireDate:[NSDate distantFuture]];

        [self pauseLayer:_iconView.layer];
    }
    else
    {
        [[PlayManager sharedManager]play];
        [self.startButton setImage:[UIImage imageNamed:@"stop"] forState:UIControlStateNormal];
        [_timer setFireDate:[NSDate distantPast]];
        
        [self resumeLayer:_iconView.layer];
    }
}

- (void)last
{
    [[PlayManager sharedManager]preMusic];
    NSInteger index = [PlayManager sharedManager].index;
    _model = _modelArray[index];
    [self getView];
    for (musicRunModel * model in _modelArray) {
        model.isChoose = NO;
    }
    _model.isChoose = YES;
    
}

- (void)next
{
    [[PlayManager sharedManager]nextMusic];
    NSInteger index = [PlayManager sharedManager].index;
    _model = _modelArray[index];
    
    [self getView];
    
    for (musicRunModel * model in _modelArray) {
        model.isChoose = NO;
    }
    _model.isChoose = YES;
    
}

- (void)playerPlaying
{
    
    if (!_isFirstPlay && [[PlayManager sharedManager]isPlaying]) {
        [self an];
        [self.startButton setImage:[UIImage imageNamed:@"stop"] forState:UIControlStateNormal];
        _isFirstPlay = YES;
    }
    
    
    
    NSInteger currentTime = [[PlayManager sharedManager] currentTime];
    NSInteger finishTime = [[PlayManager sharedManager] finishTime];
    _startLabel.text = [NSString stringWithFormat:@"%02ld:%02ld", currentTime / 60, currentTime %60];
    _endLabel.text = [NSString stringWithFormat:@"%02ld:%02ld", finishTime / 60, finishTime %60];
    
    if (currentTime == finishTime && finishTime != 0)
    {
        [[PlayManager sharedManager] playDidFinish];
    }
    
    _slider.minimumValue = 0;
    _slider.maximumValue = finishTime;
    // 在拖动的时候 不会影响播放
    _slider.continuous = NO;
    
    // 判断滑竿有没被点击
    if (_slider.isHighlighted == YES)
    {
        return;
    }
    _slider.value = currentTime;
    
}







- (void)an
{
    CABasicAnimation  *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    // 最终值
    animation.toValue = [NSNumber numberWithFloat:M_PI * 2.0 ];
    
    // 设置动画时间
    animation.duration = 10;
    
    // 设置代理 只有设置了代理 动画完成之后 才会调用didStop方法
    animation.delegate = self;
    
    // 设置动画执行次数
    animation.repeatCount = FLT_MAX;
    
    // 将view的layer添加动画
    
    [_iconView.layer addAnimation:animation forKey:nil];
}



#pragma mark ----- 暂停动画 -----
- (void)pauseLayer:(CALayer *)layer
{
    CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
    layer.speed = 0.0;
    layer.timeOffset = pausedTime;
}


#pragma mark ---- 继续动画 -----
- (void)resumeLayer:(CALayer *)layer
{
    CFTimeInterval pausedTime = [layer timeOffset];
    layer.speed = 1.0;
    layer.timeOffset = 0.0;
    layer.beginTime = 0.0;
    CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    layer.beginTime = timeSincePause;
}


#pragma mark --- 动画完成之后自动调用的方法 ---
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    CAPropertyAnimation *animation = (CAPropertyAnimation *)anim;
    
    if ([animation.keyPath isEqualToString:@"transform.rotation.z"])
    {
        [self an];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (IBAction)back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)getView
{
    // 界面赋值
    self.iconView.frame =CGRectMake(KWIDTH /4 ,KWIDTH / 4, KWIDTH /2, KWIDTH/2);
    [self.iconView.layer setCornerRadius:KWIDTH / 4];
    self.iconView.clipsToBounds = YES;
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:_model.albumUrl]];
    
    
    self.startLabel.frame = CGRectMake(20, self.iconView.frame.origin.y + KWIDTH/2 + 40, 60, 20);
    self.startLabel.text = @"00:00";
    
    
    self.slider.frame = CGRectMake(90, self.iconView.frame.origin.y + KWIDTH/2 + 40, KWIDTH - 180, 20);
    
    self.endLabel.frame = CGRectMake(KWIDTH - 90, self.iconView.frame.origin.y + KWIDTH/2 + 40, 60, 20);
    
    
    self.startButton.frame = CGRectMake((KWIDTH - 40)/2, _iconView.frame.origin.y + KWIDTH/2 + 70, 40, 40);
    
    self.lastButton.frame = CGRectMake(((KWIDTH/2)-30)/2, _iconView.frame.origin.y + KWIDTH/2 + 75, 30, 30);
    
    self.nextButton.frame = CGRectMake(((KWIDTH/2)-30)/2+KWIDTH/2, _iconView.frame.origin.y + KWIDTH/2 + 75, 30, 30);
    
    
    self.nameLabel.frame = CGRectMake(20, _iconView.frame.origin.y + KWIDTH/2 + 120, KWIDTH-40, 30);
    
    
    self.author.frame = CGRectMake(20, _iconView.frame.origin.y + KWIDTH/2 + 150, KWIDTH-40, 30);
    
    
    self.nameLabel.text = _model.name;
    self.author.text  = _model.author;
    
    
    
    self.content.text = _model.introduce;
    
    
    // 自适应高度
    CGRect rect = [self.content.text boundingRectWithSize:CGSizeMake(KWIDTH-40, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14],NSFontAttributeName, nil ] context:nil];
    self.content.textColor = [UIColor lightGrayColor];
    self.content.frame = CGRectMake(20, _iconView.frame.origin.y + KWIDTH/2 + 190, KWIDTH-40, rect.size.height);
    
    self.content.numberOfLines = 0;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
